package com.ego.coupon.controller;

import com.ego.common.to.SeckillRespTo;
import com.ego.common.util.Result;
import com.ego.coupon.service.SeckillService;
import com.ego.coupon.to.SeckillSkuInfoTo;
import com.ego.common.vo.coupon.SeckillParam;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @program: ego
 * @description: 秒杀
 * @author: KamTang
 * @create: 2022-12-31 14:10
 **/
@RestController
@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 获取最近秒杀的商品
     */
    @GetMapping("/currentSeckillSku")
    public Result getCurrentSeckillSku() {
        List<SeckillSkuInfoTo> skuTos = seckillService.getCurrentSeckillSku();
        return Result.ok().put("skuInfoTo", skuTos);
    }

    /**
     * 获取秒杀商品信息 —— 用于前端展示商品是否参与秒杀
     */
    @GetMapping("/sku/{skuId}")
    public Result getSeckillSku(@PathVariable("skuId") Long skuId) {
        SeckillSkuInfoTo seckillSkuInfoTo = seckillService.getSeckillSkuBySkuId(skuId);
        return Result.ok().setData(seckillSkuInfoTo);
    }

    /**
     * 秒杀
     */
    @PostMapping("/kill")
    public Result kill(@Valid @RequestBody SeckillParam seckillParam) {
        SeckillRespTo seckillRespTo = seckillService.kill(seckillParam);
        if (ObjectUtils.isNotEmpty(seckillRespTo)) {
            return Result.ok().setData(seckillRespTo);
        } else {
            return Result.error();
        }
    }
}
